# Replication Files for: Schenoni, Luis, Gary Goertz, Andrew P. Owsiak, and Paul F. Diehl. 2023. The Saavedra Lamas Peace: How a Norm Complex Evolved and Crystallized to Dramatically Reduce Militarized Conflict in the Americas. International Studies Quarterly.

log_file <- "my_log_file.txt"
sink(log_file)


install.packages("haven")
install.packages("dplyr")
install.packages("NCA")

#Table 2

#Table 2, first row
library(haven)
table2a <- read_dta("Table2_replication_1.dta")
library(dplyr)
subset_data <- table2a %>% filter(yearobs == 1)

t_test_result <- t.test(americas_mids_normalized ~ pre1933, data = subset_data)
print(t_test_result)

t_test_result <- t.test(other_mids_normalized ~ pre1933, data = subset_data)
print(t_test_result)

#Table 2, second row

table2b <- read_dta("Table2_replication_2.dta")
subset_data <- table2b %>% filter(americas == 1)
t_test_result <- t.test(iswb ~ pre1933, data = subset_data)
print(t_test_result)

table2b <- table2b %>% mutate(americas = ifelse(is.na(americas), 0, americas))
subset_data <- table2b %>% filter(americas == 0)
t_test_result <- t.test(iswb ~ pre1933, data = subset_data)
print(t_test_result)

#Table 2, third row

table2b <- read_dta("Table2_replication_2.dta")
subset_data <- table2b %>% filter(americas == 1)
t_test_result <- t.test(maxMIDdur ~ pre1933, data = subset_data)
print(t_test_result)

table2b <- table2b %>% mutate(americas = ifelse(is.na(americas), 0, americas))
subset_data <- table2b %>% filter(americas == 0)
t_test_result <- t.test(maxMIDdur ~ pre1933, data = subset_data)
print(t_test_result)


# Figure 2
# Barcharts for arbitration. 
# Owsiak, Andrew P., Allison Cuttner, and Brent Buck. 2018. “The International Border Agreements Dataset.” Conflict Management and Peace Science 35 (5): 559-576.

library(haven)
arbitrations <- read_dta("IBADarbitrations.dta")
arbitrations$decade <- (arbitrations$intstartyear %/% 10) * 10
arbi <- subset(arbitrations, decade >= 1830 & arbitration==1)
arbiame <- subset(arbi, statea < 200 & stateb < 200)
frequency_table <- table(arbiame$decade, arbiame$arbitration)
print(frequency_table)

y <- c("1830","1840","1850","1860","1870","1880","1890","1900","1910","1920","1930","1940","1950","1960","1970","1980","1990","2000")

s <- c(1,5,11,10,6,11,25,26,5,5,4,0,0,1,0,9,9,0)
barplot(s, names.arg=y,ylim=c(0,30), xlab="Year",ylab="Number of Ongoing Territorial Arbitrations",col="white",
        main="Arbitrations Within the Americas",border="black")

arbirest <- subset(arbi, statea > 199 | stateb > 199)
frequency_table <- table(arbirest$decade, arbirest$arbitration)
print(frequency_table)

p <- c(1,10,0,0,2,1,7,5,0,5,2,3,2,1,0,4,0,0)
barplot(p, names.arg=y,ylim=c(0,30) , xlab="Year",ylab="Number of Ongoing Territorial Arbitrations",col="white",
        main="Arbitrations Outside the Americas",border="black")

# Figure 3
# Barcharts for territorial disputes.
# Owsiak, Andrew P., Allison Cuttner, and Brent Buck. 2018. “The International Border Agreements Dataset.” Conflict Management and Peace Science 35 (5): 559-576.
# Frederick, Bryan, Paul Hensel, and Christopher Macaulay. 2017. “The Issue Correlates of War Territorial Claims Data, 1816-2001.” Journal of Peace Research 54 (1): 99-108.

library(haven)
settlements <- read_dta("IBAD-ICOWsettlements.dta")
settlements <- subset(settlements, settlements$year >= 1830)
settlements <- subset(settlements, settlements$ibadsettled >= 0)
library(dplyr)
settlements$decade <- (settlements$year %/% 10) * 10

result2 <- aggregate(settlements$noicowclaim ~ settlements$decade, settlements = df, FUN = mean)
barplot(result2$`settlements$noicowclaim`, names.arg=result2$`settlements$decade`, ylim=c(0,1) , xlab="Year",ylab="Proportion Pending no Dispute",col="white",
        main="Borders with no Pending Dispute",border="black")

result <- aggregate(settlements$ibadsettled ~ settlements$decade, settlements = df, FUN = mean)
barplot(result$`settlements$ibadsettled`, names.arg=result$`settlements$decade`, ylim=c(0,1), xlab="Year",ylab="Proportion Settled",col="white",
        main="Borders Settled",border="black")

# Figure 4
# Line graph with the proportion of disputes related to territory and unrelated to SLT norms
# Goertz, Gary, Paul F. Diehl, Andrew P. Owsiak,  and Luis Schenoni. 2023. “Tracking the Evolution of Conflict: Barometers for Interstate and Civil Conflict.” Discussion Paper Series 23-004. Washington, DC: United State Institute of Peace.

library(haven)

# "Proportion of Disputes Involving Territory"

usip21d6 <- read_dta("usip21d6.dta")

plot(usip21d6$decade,usip21d6$propterr, type="l", col="black", lwd=2, ylim=c(0,1), xlim=c(1840,2000), xlab="Year", ylab="Proportion of all MIDs")

abline(v = 1933, col = "black")

lines(usip21d6$decade, usip21d6$propother, col="black", lwd=2, lty = 3)

title()

legend(x = "topright", c("challenges territorial integrity","unrelated to SL norms"), lty = c(1, 3), cex = .8, col = c("black"), lwd = 2)


# Figure 5
# Scatterplot for territorial MIDs severity with NCA analysis
# Goertz, Gary, Paul F. Diehl, Andrew P. Owsiak,  and Luis Schenoni. 2023. “Tracking the Evolution of Conflict: Barometers for Interstate and Civil Conflict.” Discussion Paper Series 23-004. Washington, DC: United State Institute of Peace.

library(haven)

usip21d5 <- read_dta("usip21d5.dta")

library(dplyr)


terr1 = filter(usip21d5, revtype1 == 4 & nationa < 199 & nationb < 199 | revtype1 == 1 & nationa < 199 & nationb < 199)

plot(iswb ~ year, terr1, ylab = "Severity", xlab = "Year", xlim = c(1820, 2010), ylim = c(0, 250), pch = 19)


library(NCA)
modelF<-nca_analysis(terr1, 26, 21, flip.x = T, test.rep=1000)
slopeF<- -1*modelF$summaries$`year`$params[23]
interceptF<-modelF$summaries$`year`$params[24]
abline(interceptF,-1*slopeF,col="black", lwd = 2)

modelF


X = 26
Y = 21
model.flip <- nca_analysis(terr1, X, Y, ceilings = "ce_fdh", flip.x=T)
plot.flip <- model.flip$plots[[1]]
line.flip <- plot.flip$lines[["ce_fdh"]]
lines(line.flip[[1]], line.flip[[2]], type="l", lty=5, col="black", lwd=1.5)


# Figure 6
# Barcharts for overt interventions. Interventions by dacade are coded by hand based on the sources.
# Congressional Research Service. 2020. “Instances of Use of United States Armed Forces Abroad, 1798-2020.” CRS Report No. R42738. Available at: https://crsreports.congress.gov.

y <- c("1830","1840","1850","1860","1870","1880","1890","1900","1910","1920","1930","1940","1950","1960","1970","1980","1990","2000")

q <- c(6,5,9,8,4,3,7,8,10,7,2,0,0,1,0,3,1,1)
barplot(q, names.arg=y,ylim=c(0,10) , xlab="Year",ylab="Number of Overt Interventions",col="white",
        main="Overt Interventions Active",border="black")

r <- c(4,2,7,5,2,2,7,7,10,7,2,0,0,1,0,2,1,1)
barplot(r, names.arg=y,ylim=c(0,10) , xlab="Year",ylab="Number of US Overt Interventions",col="white",
        main="US Overt Interventions Active",border="black")



# Figure 7
# Line graph with the proportion of disputes related to intervention and unrelated to SLT norms
# Goertz, Gary, Paul F. Diehl, Andrew P. Owsiak,  and Luis Schenoni. 2023. “Tracking the Evolution of Conflict: Barometers for Interstate and Civil Conflict.” Discussion Paper Series 23-004. Washington, DC: United State Institute of Peace.

#"Proportion of Disputes Involving Intervention"
library(haven)

usip21d6 <- read_dta("usip21d6.dta")

plot(usip21d6$decade,usip21d6$propinterv, type="l", col="black", lwd=2, ylim=c(0,1), xlim=c(1840,2000), xlab="Year", ylab="Proportion of all MIDs")

abline(v = 1933, col = "black")

lines(usip21d6$decade, usip21d6$propother, col="black", lwd=2, lty = 3)

title()

legend(x = "topright", c("challenges non-intervention","unrelated to SL norms"), lty = c(1, 3), cex = .8, col = c("black"), lwd = 2)


# Figure 8
# Scatterplot for intervention MIDs severity with NCA analysis
# Goertz, Gary, Paul F. Diehl, Andrew P. Owsiak,  and Luis Schenoni. 2023. “Tracking the Evolution of Conflict: Barometers for Interstate and Civil Conflict.” Discussion Paper Series 23-004. Washington, DC: United State Institute of Peace.

library(haven)

usip21d3 <- read_dta("usip21d3.dta")

library(dplyr)


forint = filter(usip21d3, nationa < 199 & nationb == 2 | nationa == 2 & nationb < 199 | nationa < 199 & nationb == 200 | nationa == 200 & nationb < 199 | nationa < 199 & nationb == 220 | nationa == 220 & nationb < 199 | nationa < 199 & nationb == 230 | nationa == 230 & nationb < 199)

plot(iswb ~ year, forint, ylab = "Severity", xlab = "Year", xlim = c(1820, 2010), ylim = c(0, 250), pch = 19)

library(NCA)
modelF<-nca_analysis(forint, 26, 21, flip.x = T, test.rep=1000)
slopeF<- -1*modelF$summaries$`year`$params[23]
interceptF<-modelF$summaries$`year`$params[24]
abline(interceptF,-1*slopeF,col="black", lwd = 2)

modelF



X = 26
Y = 21
model.flip <- nca_analysis(forint, X, Y, ceilings = "ce_fdh", flip.x=T)
plot.flip <- model.flip$plots[[1]]
line.flip <- plot.flip$lines[["ce_fdh"]]
lines(line.flip[[1]], line.flip[[2]], type="l", lty=5, col="black", lwd=1.5)



sink()




